package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品规格 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "商品规格")
@RestController
@RequestMapping("/productSpec")
public class ProductSpecController extends BaseController {

    @Autowired
    private ProductSpecService productSpecService;

    @Operation(summary = "分页及带条件查询")
    @GetMapping("/list")
    public TableDataInfo getPageInfo(ProductSpec productSpec){
        //开启分页
        startPage();
        //调用ProductSpecService中的方法
        List<ProductSpec> productSpecList = productSpecService.getPageInfo(productSpec);
        return getDataTable(productSpecList);
    }

    @Operation(summary = "新增规格信息")
    @PostMapping
    public AjaxResult saveProductSpec(@RequestBody ProductSpec productSpec){
        boolean save = productSpecService.save(productSpec);
        return toAjax(save);
    }

    @Operation(summary = "根据Id查询规格信息")
    @GetMapping("/{id}")
    public AjaxResult getProductSpec(@PathVariable Long id){
        ProductSpec byId = productSpecService.getById(id);
        return success(byId);
    }

    @Operation(summary = "根据Id修改规格信息")
    @PutMapping
    public AjaxResult updateProductSpec(@RequestBody ProductSpec productSpec){
        boolean update = productSpecService.saveOrUpdate(productSpec);
        return toAjax(update);
    }

    @Operation(summary = "根据Ids删除规格信息")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProductSpec(@PathVariable List<Long> ids){
        boolean batchByIds = productSpecService.removeBatchByIds(ids);
        return toAjax(batchByIds);
    }

    @Operation(summary = "根据分类ID查询规格信息")
    @GetMapping("/productSpecList/{id}")
    public AjaxResult getProductSpecListByCategoryId(@PathVariable Long id){
        LambdaQueryWrapper<ProductSpec> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductSpec::getCategoryId,id);
        List<ProductSpec> list = productSpecService.list(wrapper);
        return AjaxResult.success(list);
    }

}
